package com.tedu.controller;

import com.tedu.pojo.Cate;
import com.tedu.pojo.Item;
import com.tedu.pojo.ItemDesc;
import com.tedu.pojo.User;
import com.tedu.servies.CateServices;
import com.tedu.servies.ItemDescServies;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class CateController {
    @Autowired
    CateServices cateServices;
    @Autowired
    ItemDescServies itemDescServies;

    //显示购物车列表页面
    @RequestMapping("/toCate")
    public String toCate(HttpSession session, Model model){
        //判断用户是否登录   用户未登录，则跳转到登录页面
        User u = (User) session.getAttribute("user");
        System.out.println(u.getId());
        //未登录    登录页面

        //通过用户的id获取购物车记录
        List<Cate> data = cateServices.getListByUserId(u.getId());
        System.out.println("xxxx");
        System.out.println(data);
        //从data中 获取商品id列表
        List<Long> idList = new ArrayList<Long>();
        for (int i = 0; i < data.size(); i++) {
            idList.add(data.get(i).getItemId());
        }
        System.out.println(idList);
        List<Item> itemByIDlist = itemDescServies.getItemByIDlist(idList);

        model.addAttribute("itemVOs",itemByIDlist);

        return "cart";
    }
}
